<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
      
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="公司全称">
          <a-input placeholder="请选择公司" v-show v-decorator="['CompanyId', validatorRules.companyid]"></a-input>
          <a-input placeholder="请选择公司" @click="showCommonModal" v-decorator="['Company_CompanyName',{}]"></a-input>
          <!-- <a-select show-search :filter-option="filterOption" placeholder="请选择公司" v-decorator="['CompanyId', validatorRules.companyid]" >
            <a-select-option value="">请选择</a-select-option>
            <a-select-option :value="item.CompanyId" v-for="(item,index) in companyData">{{item.CompanyName}}</a-select-option>
          </a-select> -->
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="融资轮次">
          <a-select placeholder="请输入融资轮次" @change="changeRef" v-decorator="['RefinanceRoundId', validatorRules.refinanceroundid]" >
            <a-select-option value="">请选择</a-select-option>
            <a-select-option :value="item.BaseInfoId" v-for="(item,index) in refinanceRoundData">{{item.Value}}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          v-if="model.RefinanceRoundId != '36'"
          label="融资日期">
          <a-date-picker placeholder="请输入融资日期" v-decorator="['RefinanceDate', validatorRules.refinancedate]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          v-if="model.RefinanceRoundId != '36'"
          label="融资币种">
          <a-select @change="changeCType" placeholder="请输入融资币种" v-decorator="['CurrencyTypeId', validatorRules.currencytypeid]" >
            <a-select-option value="">请选择</a-select-option>
            <a-select-option :value="item.BaseInfoId" v-for="(item,index) in currentTypeData">{{item.Value}}</a-select-option>
          </a-select>
        </a-form-item>
        
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          v-if="model.RefinanceRoundId != '36'"
          label="融资金额">
          <a-input style="width:200px;" placeholder="请输入融资金额" v-decorator="['RefinanceAmt', validatorRules.refinanceamt]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          v-if="model.RefinanceRoundId != '36'"
          label="真实融资">
          <a-input-number style="width:200px;" placeholder="请输入真实融资" v-decorator="['RefinanceTrueAmt', {}]" />
          <span style="margin-left: 5px;">万</span>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          v-if="model.RefinanceRoundId != '36'"
          label="投后估值">
          <a-input-number style="width:200px;" placeholder="请输入投后估值" v-decorator="['ValuationAmt', {}]" />
          <span style="margin-left: 5px;">亿元</span>
        </a-form-item>
        

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          v-if="model.RefinanceRoundId != '36'"
          label="投资方">
          <a-input placeholder="请选择投资方" v-show v-decorator="['Investor', validatorRules.investorid]"></a-input>
          <a-input placeholder="请选择投资方" @click="showCommonModaltz" v-decorator="['InvestorName',{}]"></a-input>
          <!-- <a-select mode="multiple" placeholder="请输入投资方" v-decorator="['Investor', validatorRules.investorid]" >
            <a-select-option value="">请选择</a-select-option>
            <a-select-option :value="item.CompanyId" v-for="(item,index) in investorData">{{item.CompanyName}}</a-select-option>
          </a-select> -->
        </a-form-item>
		    <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="是否已对外公布">
          <a-radio-group name="radioGroup" v-decorator="['IsDisclose',{rules:[{required:true,message:'请填写'}]}]" :default-value="1">
            <a-radio :value="1">
              公布
            </a-radio>
            <a-radio :value="0">
              不公布
            </a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="是否有股东变更">
          <a-radio-group name="radioGroup" v-decorator="['IsShareholderChanged',{rules:[{required:true,message:'请填写'}]}]" :default-value="0">
            <a-radio :value="1">
              有
            </a-radio>
            <a-radio :value="0">
              无
            </a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="是否有注册资本变更">
          <a-radio-group name="radioGroup" v-decorator="['IsRegisteredAmtChanged',{rules:[{required:true,message:'请填写'}]}]" :default-value="0">
            <a-radio :value="1">
              有
            </a-radio>
            <a-radio :value="0">
              无
            </a-radio>
          </a-radio-group>
        </a-form-item>
      </a-form>
    </a-spin>
    <commonModal
      modalTitle="公司选择"
      ref="commonModal"
      @getCommonModalData="commonModalData"
      :columns="tableColumns"
      apiUrl="/companyInfo/queryRows"
      :searchArr="searchArr"></commonModal>
      <commonModal
      modalTitle="投资方选择"
      ref="commonModaltz"
      selectType="checkbox"
      @getCommonModalData="commonModalDatatz"
      :columns="tableColumnstz"
      apiUrl="/companyInfo/queryRows"
      :searchArr="searchArrtz"></commonModal>
  </a-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"
  import commonModal from '@/components/commonModal'
  export default {
    name: "CompanyRefinanceModal",
    components:{
      commonModal
    },
    data () {
      return {
        tableColumns:[
          { title: '公司全称', dataIndex: 'CompanyName' },
          { title: '公司编码', dataIndex: 'CompanyCode' },
        ],
        searchArr:[
          { label: '公司全称', key: 'CompanyName' },
          { label: '公司编码', key: 'CompanyCode' },
        ],
        tableColumnstz:[
          { title: '公司全称', dataIndex: 'CompanyName' },
          // { title: '公司简称', dataIndex: 'CompanyAbbr' },
          { title: '公司编码', dataIndex: 'CompanyCode' },
        ],
        searchArrtz:[
          { label: '公司简称', key: 'CompanyAbbr' },
          { label: '公司编码', key: 'CompanyCode' },
        ],
        investorData:[],
        currentTypeData:[],
        refinanceRoundData:[{
          id:'36',
          name:'尚未获投'
        },{
          id:'37',
          name:'种子轮'
        },{
          id:'38',
          name:'天使轮'
        },{
          id:'39',
          name:'Pre-A轮'
        },{
          id:'40',
          name:'A轮'
        },{
          id:'41',
          name:'A+轮'
        },{
          id:'42',
          name:'Pre-B轮'
        },{
          id:'43',
          name:'B轮'
        },{
          id:'44',
          name:'B+轮'
        },{
          id:'45',
          name:'C轮'
        },{
          id:'46',
          name:'C+轮'
        },{
          id:'47',
          name:'D轮'
        },{
          id:'48',
          name:'D+轮'
        },{
          id:'49',
          name:'E轮'
        },{
          id:'50',
          name:'F轮-上市前'
        },{
          id:'51',
          name:'已上市'
        },{
          id:'52',
          name:'新三板'
        },{
          id:'53',
          name:'战略投资'
        },{
          id:'55',
          name:'已被收购'
        },{
          id:'56',
          name:'已退市'
        },{
          id:'57',
          name:'不明确'
        }],
        companyData:[],
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        companyrefinanceid:{rules: [{ required: true, message: '请输入融资历程ID!' }]},
        companyid:{rules: [{ required: true, message: '请选择公司!' }]},
        currencytypeid:{rules: [{ required: true, message: '请输入融资币种!' }]},
        refinancedate:{rules: [{ required: true, message: '请输入融资日期!' }]},
        refinanceamt:{rules: [{ required: true, message: '请输入融资金额!' }]},
        refinancetrueamt:{rules: [{ required: true, message: '请输入真实融资!' }]},
        updatebyid:{rules: [{ required: true, message: '请输入最近更新人!' }]},
        investor:{rules: [{ required: true, message: '请输入!' }]},
        valuationamt:{rules: [{ required: true, message: '请输入投后估值!' }]},
        refinanceroundid:{rules: [{ required: true, message: '请输入融资轮次!' }]},
        updateby:{rules: [{ required: true, message: '请输入最近更新人!' }]},
        ts:{rules: [{ required: true, message: '请输入最近更新日期!' }]},
        datastatus:{rules: [{ required: true, message: '请输入状态!' }]},
        investorid:{rules: [{ required: true, message: '请输入投资方!' }]},
        },
        moneyUnit:'元',
        url: {
          add: "/companyRefinance/add",
          edit: "/companyRefinance/update",
          baseList:"/baseInfo/queryRows",
        },
      }
    },
    created () {
      // this.getCompany();
      this.getBaseData();
    },
    methods: {
      changeRef(value){
        this.model.RefinanceRoundId = value;
      },
      showCommonModal () {
        this.$refs.commonModal.open()
      },
      commonModalData(val){
        this.form.setFieldsValue({
          CompanyId:val[0].CompanyId,
          Company_CompanyName:val[0].CompanyName
        })
      },
      showCommonModaltz () {
        this.$refs.commonModaltz.open()
      },
      commonModalDatatz(val){
        let ids = [];
        let names = [];
        val.forEach(e=>{
          ids.push(e.CompanyId);
          names.push(e.CompanyName);
        })
        this.form.setFieldsValue({
          Investor:ids.join(','),
          InvestorName:names.join(',')
        })
      },
      filterOption(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },
      getBaseData(){
        this.$http.postAction(this.url.baseList,{pageIndex:1,pageSize:100,selectFields:[],orders:[{field:'OrderNum',desc:false}],conditions:[{
              "field":"TypeCode",
              "operator":"=",
              "value":'CurrencyType'
            }]}).then((res)=>{
          if(res.status == 1){
            this.currentTypeData = res.data.rows;
          }
        })
        this.$http.postAction(this.url.baseList,{pageIndex:1,pageSize:100,selectFields:[],orders:[{field:'OrderNum',desc:false}],conditions:[{
              "field":"TypeCode",
              "operator":"=",
              "value":'RefinanceRound'
            }]}).then((res)=>{
          if(res.status == 1){
            this.refinanceRoundData = res.data.rows;
          }
        })
        // this.$http.postAction('/companyInfo/queryRows',{pageIndex:1,pageSize:1000,selectFields:[]}).then((res)=>{
        //   if(res.status == 1){
        //     this.investorData = res.data.rows;
        //   }
        // })
      },
      changeCType(id){
        this.currentTypeData.forEach((e)=>{
          if(e.BaseInfoId == id){
            this.moneyUnit = e.Value;
          }
        })
        this.$forceUpdate();
      },
      getCompany(){
        this.$http.postAction('/companyInfo/queryRows',{pageIndex:1,pageSize:9999}).then((res)=>{
          if(res.status == 1){
            this.companyData = res.data.rows;
          }
        })
      },
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.model.CompanyId = this.model.Company;
        this.model.CurrencyTypeId = this.model.CurrencyType;
        this.model.RefinanceRoundId = this.model.RefinanceRound;
        this.model.Investor = this.model.InvestorIds || '';
        this.model.InvestorName = this.model.Investors || '';
        // this.model.IsDisclose = this.model.IsDisclose || 1;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'CompanyId','CurrencyTypeId','RefinanceAmt','RefinanceTrueAmt','Investor','InvestorName','ValuationAmt','RefinanceRoundId','Company_CompanyName','IsDisclose', 'IsRegisteredAmtChanged', 'IsShareholderChanged'))
		  //时间格式化
      this.form.setFieldsValue({RefinanceDate:this.model.RefinanceDate?moment(this.model.RefinanceDate,'YYYY-MM-DD HH:mm:ss'):null})
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.CompanyRefinanceId){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            formData.RefinanceDate = formData.RefinanceDate?formData.RefinanceDate.format('YYYY-MM-DD'):null;
            console.log(formData)
            formData.RefinanceTrueAmt = formData.RefinanceTrueAmt?formData.RefinanceTrueAmt:0;
            formData.ValuationAmt = formData.ValuationAmt?formData.ValuationAmt:0;
            // formData.Investor = formData.Investor instanceof Array?formData.Investor.join():formData.Investor;
            // formData.Investors = [];
            // if(formData.Investor instanceof Array){
            //   formData.Investor.forEach(e=>{
            //     formData.Investors.push({InvestorId:e});
            //   })
            // }else{
            //   if(formData.Investor){
            //     formData.Investors.push({InvestorId:formData.Investor});
            //   }
            // }
            formData.Investors = [];
            if(formData.Investor){
              formData.Investor.split(',').forEach(e=>{
                formData.Investors.push({InvestorId:e});
              })
            }
            this.$http.postAction(httpurl,formData,method).then((res)=>{
              if(res.status == 1){
                that.$message.success('提交成功');
                if(method == 'post'){
                  this.form.resetFields();
                  this.model = {};
                }else{
                  that.close();
                }
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              // that.close();
            })



          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>

</style>